<?php namespace App\Http\Controllers\Api;

use App\Goods;
use App\GoodsImg;
use Illuminate\Support\Facades\Input;

class GoodsController extends ApiController
{
    public function createGood(){
        $goodName  = Input::get('name');
        $mainImg   = Input::get('main_img');
        $thumbImg1 = Input::get('thumbimg1');
        $thumbImg2 = Input::get('thumbimg2');
        $stock     = Input::get('stock');
        $express   = Input::get('express');
        $info      = Input::get('info');
        $m_price   = Input::get('m_price');
        $s_price   = Input::get('s_price');
        $id        = !is_null(Input::get('id')) ? Input::get('id') : 0;

        $goodModel = Goods::find($id);
        if(!$goodModel)
            $goodModel = new Goods();

        if(!is_null($goodName))$goodModel->name = $goodName;
        if(!is_null($mainImg))$goodModel->thumb_id = $mainImg;
        if(!is_null($stock))$goodModel->stock = $stock;
        if(!is_null($express))$goodModel->express_fee = $express;
        if(!is_null($info))$goodModel->info = $info;
        if(!is_null($m_price))$goodModel->m_price = $m_price;
        if(!is_null($s_price))$goodModel->s_price = $s_price;
        $goodModel->save();

        $thatThumb1 = GoodsImg::find($thumbImg1);
        if(!$thatThumb1)
            $thatThumb1 = new GoodsImg();

        if(!is_null($thumbImg1)){
            $thatThumb1->thumb_id = $thumbImg1;
            $thatThumb1->good_id  = $goodModel->id;
            $thatThumb1->save();
        }

        $thatThumb2 = GoodsImg::find($thumbImg2);
        if(!$thatThumb2)
            $thatThumb2 = new GoodsImg();

        if(!is_null($thumbImg2)){
            $thatThumb2->thumb_id = $thumbImg2;
            $thatThumb2->good_id  = $goodModel->id;
            $thatThumb2->save();
        }

        return $this->buildResponse(trans('添加成功'),$goodModel);
    }
}